package cn.edu.xaut.mybatisers.mall.Controller;

import cn.edu.xaut.mybatisers.mall.Entity.Consumer;
import cn.edu.xaut.mybatisers.mall.Exception.ExceptionEntity.LoginException;
import cn.edu.xaut.mybatisers.mall.Exception.ExceptionEntity.NotMemberException;
import cn.edu.xaut.mybatisers.mall.Exception.ExceptionEntity.ParametersIncompleteException;
import cn.edu.xaut.mybatisers.mall.Exception.ExceptionEntity.TeleNumberExistException;
import cn.edu.xaut.mybatisers.mall.Service.RegisterService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.mail.MailException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.regex.Pattern;

@Controller
public class RegisterController {
    @Autowired
    RegisterService registerService;
    @ApiOperation("注册为普通用户(也就是创建用户)(表单数据)")
    @RequestMapping(value = "/register/user", method = RequestMethod.POST)
    @ResponseBody
    public Boolean registerUser(Consumer consumer, String verifyCode, HttpSession session) throws ParametersIncompleteException, SQLException, TeleNumberExistException, LoginException {
        if (consumer.getAccountMember() == null || consumer.getPassword() == null){
            throw new ParametersIncompleteException("AccountMember||password");
        }
        if (!Pattern.compile("^1[3,5]\\d{9}||18[6,8,9]\\d{8}$").matcher(consumer.getAccountMember()).matches()){
            throw new ParametersIncompleteException("AccountMember不正确");
        }
        if (consumer.getEmail() == null){
            throw new ParametersIncompleteException("Email不完整");
        }
        registerService.normalConsumer(consumer, verifyCode, session);
        return true;
    }


    @ApiOperation(value = "只有注册的时候,发送邮箱验证码")
    @RequestMapping(value = "/register/sendVerifyCode", method = RequestMethod.GET)
    @ResponseBody
    public Boolean sendVerifyCode(Consumer consumer, HttpServletRequest request, HttpSession session) throws SQLException, MailException, NotMemberException, LoginException, ParametersIncompleteException {
        /***
         *      1. 用户注册的时候只传用户输入发邮箱
         *      2. 找密码时只传id，用用户以前注册的邮箱发邮件
         * */
        return registerService.sendVerifyCode(consumer, request, session);
    }

    @ApiOperation(value = "修改密码时,发送邮箱验证码")
    @RequestMapping(value = "/modifyPassword/sendVerifyCode", method = RequestMethod.GET)
    @ResponseBody
    public Boolean sendModifyPasswordVerifyCode(Consumer consumer, HttpServletRequest request, HttpSession session) throws SQLException, MailException, NotMemberException, LoginException, ParametersIncompleteException {
        /***
         *      1. 用户注册的时候只传用户输入发邮箱
         *      2. 找密码时只传id，用用户以前注册的邮箱发邮件
         * */
        return registerService.sendVerifyCode(consumer, request, session);
    }

    @ApiOperation("修改密码(表单数据)")
    @RequestMapping(value = "/modifyPassword", method = RequestMethod.POST)
    @ResponseBody
    public Boolean modifyPassword(Consumer consumer, String newPassword, String verifyCode, HttpSession session) throws ParametersIncompleteException, SQLException, LoginException {
        if (consumer.getPassword() == null || newPassword == null){
            throw new ParametersIncompleteException("consumer || password");
        }
        registerService.modifyPassword(consumer, newPassword, verifyCode, session);
        return true;
    }

    @ApiOperation("认证为会员")
    @RequestMapping(value = "/register/member", method = RequestMethod.POST)
    @ResponseBody
    public Boolean authenticMember(Consumer consumer, HttpSession session) throws SQLException, ParametersIncompleteException {
        if (!Pattern.compile("^1[3,5]\\d{9}||18[6,8,9]\\d{8}$").matcher(consumer.getTelenumber()).matches()){
            throw new ParametersIncompleteException("电话号不正确");
        }
        consumer.setId((Integer) session.getAttribute("consumer_id"));
        consumer.setMember(true);
        System.out.println(consumer.getEmail());
        registerService.authenticMember(consumer);
        return true;
    }

    @ApiOperation("会员修改数据")
    @RequestMapping(value = "/modifyInfo", method = RequestMethod.POST)
    @ResponseBody
    public Boolean modifyInfo(@RequestBody Consumer consumer, HttpSession session) throws SQLException {
        consumer.setId((Integer) session.getAttribute("consumer_id"));
        registerService.modifyInfo(consumer);
        return true;
    }

    @ApiOperation("通过id获取用户信息")
    @RequestMapping(value = "/getInfo", method = RequestMethod.GET)
    @ResponseBody
    public Consumer getInfo(HttpSession session) throws SQLException {
        Consumer consumer = new Consumer();
        consumer.setId((Integer) session.getAttribute("consumer_id"));
        return registerService.getInfoById(consumer);
    }

}
